package com.xhu.collegestudentrentalsystem.module.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.xhu.collegestudentrentalsystem.module.manager.dao.NewsDAO;
import com.xhu.collegestudentrentalsystem.module.manager.entity.News;
import com.xhu.collegestudentrentalsystem.module.manager.service.INewsService;
import com.xhu.collegestudentrentalsystem.module.user.entity.Page;
import com.xhu.collegestudentrentalsystem.util.constant.EntityProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhu huilin
 * @date 2023/3/27 9:58
 */
@Service
public class NewsServiceImpl implements INewsService {

    @Autowired
    private NewsDAO newsDAO;

    /**
     * 查询所有的新闻信息
     *      按时间倒叙排列
     */
    @Override
    public List<News> findAllNews(Page page) {
        PageHelper.startPage(page.getCurrent() , page.getLimit());
        QueryWrapper<News> qw = new QueryWrapper<News>().orderByDesc(EntityProperty.CREATE_TIME);
        List<News> news = newsDAO.selectList(qw);
        Integer count = newsDAO.selectCount(qw);
        page.setRows(count);
        return news;
    }

    /**
     * 根据id查询一条新闻信息
     */
    @Override
    public News findNewsById(Long newsId) {
        return newsDAO.selectOne(new QueryWrapper<News>().eq(EntityProperty.ID, newsId));
    }

    /**
     * 根据id修改新闻信息
     */
    @Override
    public Integer updateNews(News news) {
        return newsDAO.updateById(news);
    }

    /**
     * 添加一条新闻信息
     */
    public Integer addNews(News news){
        return newsDAO.insert(news);
    }

    /**
     * 删除一条新闻信息
     */
    @Override
    public Integer deleteNewsById(Long newsId) {
        return newsDAO.delete(new QueryWrapper<News>().eq(EntityProperty.ID, newsId));
    }
}
